﻿using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Web.Models.MvcExtension
{


    /// <summary>
    /// 分页option属性
    /// </summary>
    public class PagerConfig
    {

        string _PageIDKey = "pid";
        /// <summary>
        /// 分页标记(url参数)
        /// </summary>
        public string PageIDKey
        {
            get
            {
                return _PageIDKey;
            }
            set
            {
                if (value != null)
                    _PageIDKey = value;
            }
        }
        int _PageSize = 20;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                if (value > 0 && value <= 50001)
                    _PageSize = value;
            }
        }
        int _PageIndex = -1;
        /// <summary>
        /// 当前页编号
        /// </summary>
        public int PageIndex
        {
            get
            {
                return _PageIndex;
            }
            set
            {
                _PageIndex = value;
            }
        }

        int _Records = 0;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int Records
        {
            get
            {
                return _Records;
            }
            set
            {
                this._Records = value; // == 0 ? 1 : rowCount;
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                return Math.Max(1, (this.Records + this.PageSize - 1) / this.PageSize);
            }
        }

        public string PageUrl
        {
            get; set;
        }

        public string GetPageUrl(int index)
        {
            if (this.PageUrl.IndexOf("pid=") > 0)
                return this.PageUrl.Replace("pid=" + this.PageIndex, "pid=" + index);
            else
                return this.PageUrl + "?pid=" + index;
        }
    }

    [HtmlTargetElement("pager")]
    public class PagerTagHelper : TagHelper
    {
        [ViewContext]
        [HtmlAttributeNotBound]
        public ViewContext ViewContext { get; set; }
        //对应的标签属性名称（大小写区分）
        public int PagerTotalCount { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context.TagName == "pager")
            {
                PagerConfig config = new PagerConfig();
                //string controller = ViewContext.RouteData.Values["controller"].ToString();
                //string action = ViewContext.RouteData.Values["action"].ToString();
                string queryString = ViewContext.HttpContext.Request.QueryString.Value;

                config.PageIndex = 1;
                if (ViewContext.HttpContext.Request.Query["pid"].ToString() != "")
                {
                    config.PageIndex = int.Parse(ViewContext.HttpContext.Request.Query["pid"].ToString());
                }
                config.PageUrl = ViewContext.HttpContext.Request.Path + queryString;
                config.Records = PagerTotalCount;
                StringBuilder builder = new StringBuilder();
                if (config.PageCount > 1)
                {
                    builder.Append("<nav><ul class=\"pagination\">");
                    builder.AppendFormat("<li class=\"{0}\"><a href=\"{1}\">上一页</a></li>", config.PageIndex == 1 ? "disabled" : "", config.PageIndex == 1 ? "javascript:void(0);" : config.GetPageUrl(config.PageIndex - 1));
                    int minCount = 10;
                    int start = 1;// 
                    int end = config.PageCount;// 

                    if (config.PageCount > minCount)
                    {
                        start = config.PageIndex - 5 > 0 ? config.PageIndex - 5 : 1;
                        end = config.PageIndex + 4 > config.PageCount ? config.PageCount : config.PageIndex + 4;
                        if (end - start != minCount)
                        {
                            if (start == 1) end = minCount - start + 1;
                            else start = end - minCount + 1;
                        }
                    }
                    for (; start <= end; start++)
                    {
                        if (start == config.PageIndex)
                        {
                            builder.AppendFormat("<li class=\"active\"><a href=\"javascript:void(0)\">{0}</a></li>", start.ToString());
                        }
                        else
                        {
                            builder.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", config.GetPageUrl(start), start.ToString());
                        }
                    }
                    builder.AppendFormat("<li class=\"{0}\"><a href=\"{1}\">下一页</a></li>", config.PageIndex == config.PageCount ? "disabled" : "", config.PageIndex == config.PageCount ? "javascript:void(0);" : config.GetPageUrl(config.PageIndex + 1));
                    builder.Append("</ul></nav>");
                }
                else
                {
                    if (config.Records > 0)
                    {
                        builder.Append("<nav><ul class=\"pagination\">");
                        builder.AppendFormat("<li class=\"{0}\"><a href=\"{1}\">上一页</a></li>", config.PageIndex == 1 ? "disabled" : "", config.PageIndex == 1 ? "javascript:void(0);" : config.GetPageUrl(config.PageIndex - 1));
                        builder.Append("<li class=\"active\"><a href=\"javascript:void(0)\">1</a></li>");
                        builder.AppendFormat("<li class=\"{0}\"><a href=\"{1}\">下一页</a></li>", config.PageIndex == config.PageCount ? "disabled" : "", config.PageIndex == config.PageCount ? "javascript:void(0);" : config.GetPageUrl(config.PageIndex + 1));
                        builder.Append("</ul></nav>");
                    }
                }
                output.Content.SetHtmlContent(builder.ToString());
            }
        }
    }
}
